<template>
	<view>
		<scroll-view scroll-y="true" :refresher-triggered="triggered" class="scroll-y" @refresherrefresh="handleRefresh"
			@scrolltolower="lower" :style="{height:scrollHeight}" :enhanced="true" :show-scrollbar="true">
			<view v-if="showType=='list'">
				<u-sticky>
					<view class="list head  flex-s">
						<view class="list_son">巡检开始时间</view>
						<view class="list_son">巡检结束时间</view>
						<view class="list_son">详情</view>
					</view>
				</u-sticky>
				<view class="list flex-s" v-for="(item,index) in list" :key="index">
					<view class="list_son">{{moment(item.action_start).format("YYYY-MM-DD")}}</view>

					<view class="list_son">{{item.action_end&&moment(item.action_end).format("YYYY-MM-DD") || '--'}}
					</view>
					<view class="list_son">
						<view class="btn flex-c" @click="setShowType(index)">查看</view>
					</view>
				</view>
			</view>
			<view v-else>
				<view class="card" v-for="(item,index) in list" :key="index">
					<view class="cell">
						<view class="label">巡检计划编号：</view>
						<view class="value">
							<view class="flex-s" style="width: 100%;">
								{{item.device_sns}}
								<view class="icon flex-c" :class="[openIndex==index?'open':'']" @click="setOpen(index)">
									<u-icon size="32rpx" name="arrow-up"></u-icon>
								</view>
							</view>

						</view>
					</view>
					<view class="line"></view>
					<view class="cell">
						<view class="label">状态：</view>
						<view class="value" :class="['inspection_status_'+item.inspection_status]">
							{{item.inspection_status_name}}
						</view>
					</view>
					<view v-if="openIndex==index">
						<view class="cell">
							<view class="label">巡检名称：</view>
							<view class="value">{{item.title}}</view>
						</view>
						<view class="cell">
							<view class="label">巡检对象：</view>
							<view class="value">{{item.shop_name_f}}</view>
						</view>
						<view class="cell">
							<view class="label">巡检地点：</view>
							<view class="value">{{item.address_f}}</view>
						</view>
						<view style="display: flex;flex-direction: column;gap:30rpx;margin-top:20rpx">
							<view style="color:#333;font-size:28rpx;">巡检设备：</view>
							<view
								style="display: flex;flex-direction: column;gap:30rpx;max-height:300rpx;overflow: auto;">
								<view v-for="(obj,j) in item.device_list" :key="j"
									style="display: flex;gap:10rpx;align-items: center;justify-content: space-between;">
									<view style="flex:1;font-size:28rpx;">{{obj.device_name}}</view>
									<view style="display: flex;gap:10rpx;">
										<view
											@click.stop="$func.toUrl(obj.member_describe_s!=''?'/pages_user/inspections/maintain?id='+item.id:'')"
											:style="obj.member_describe_s!=''?'background:#4F6FF6':'background:#ADADAD'"
											style='border-radius:10rpx;padding:20rpx 17rpx;font-size:30rpx;color:white'>
											保养单
										</view>
										<view
											@click.stop="$func.toUrl(obj.fault_describe !=''?'/pages_user/inspections/fault?id='+item.id:'')"
											:style="obj.fault_describe !=''?'background:#4F6FF6':'background:#ADADAD'"
											style='border-radius:10rpx;padding:20rpx 17rpx;font-size:30rpx;color:white'>
											故障单
										</view>
									</view>
								</view>
							</view>
						</view>
						<view class="cell">
							<view class="label">巡检人员：</view>
							<view class="value">{{item.name_f}}</view>
						</view>
						<view class="line"></view>


						<view class="cell">
							<view class="label">巡检反馈建议：</view>
						</view>
						<!-- 	<view class="tags flex-c">
						<view class="tags_son flex-c">
							{{item.device_name_f}}
						</view>
					</view> -->
						<view v-if="item.order_list&&item.order_list.length>0">
							<view v-for="(vo,idx) in item.order_list">
								<view class="tags flex-c">
									<view class="tags_son flex-c">{{vo.device_name}}</view>
								</view>
								<view class="textarea">
									{{vo.member_describe_s}}
								</view>
								<view class="line"></view>
								<view class="cell">
									<view class="label">巡检图片</view>
								</view>
								<view class="upload">
									<view class="imgs_list flex-c">
										<view class="imgs_son" v-for="(vos,idxx) in vo.member_imgs_ff" :key="idxx">
											<u-image :src="vos" mode="aspectFill" width="100px" height="100px"
												@click="$func.previewImage(vos,vos.member_imgs_ff)"></u-image>
										</view>
									</view>
								</view>
							</view>
						</view>
						<view v-else>
							<view class="textarea">
								{{item.member_describe}}
							</view>
							<view class="line"></view>
							<view class="cell">
								<view class="label">巡检图片</view>
							</view>
							<view class="upload">
								<view class="imgs_list flex-c">
									<view class="imgs_son" v-for="(vo,idx) in item.member_store_imgs_f" :key="idx">
										<u-image :src="vo" mode="aspectFill" width="100px" height="100px"
											@click="$func.previewImage(vo,item.member_store_imgs_f)"></u-image>
									</view>
								</view>
							</view>
						</view>


					</view>
					<view class="cell">
						<view class="label">预计巡检时间：</view>
						<view class="value">{{item.action_start}}至{{item.action_end}}</view>
					</view>
					<view class="cell">
						<view class="label">实际巡检时间：</view>
						<view class="value">{{item.dotime}}</view>
					</view>
				</view>
			</view>
			<u-divider text="暂无数据" v-if="list.length==0 && finish"></u-divider>
			<u-divider text="暂无更多数据" v-if="list.length>0 && finish"></u-divider>
			<u-divider text="加载中" v-if="loading"></u-divider>
			<view style="height: 140rpx;" v-if="showType=='list'"></view>
		</scroll-view>
		<view class="btns flex-c" v-if="showType=='list'" @click="setShowType(index)">查看全部</view>
	</view>
</template>

<script>
	import moment from "moment"
	const systemInfo = uni.getSystemInfoSync();
	const safeArea = systemInfo.safeArea;
	export default {
		name: "deviceCard",
		props: {
			info: {
				type: Object,
				default: () => {}
			},

		},
		data() {
			return {
				moment: moment,
				showType: "list",
				openIndex: -1,
				status: ['正常', '故障停机', '异常可使用', '报废'],
				loading: false,
				triggered: false,
				finish: false,
				total: 0,
				list: [],
				query: {
					page: 1,
					pageSize: 20
				},

			};
		},
		computed: {
			scrollHeight() {
				return 'calc(100vh -  102rpx)'
			},
			imgCount() {
				return (imgs) => {
					const list = imgs ? imgs.split('|') : []
					return list.length
				}
			},
			imageList() {
				return (imgs) => {
					console.log(imgs)
					const list = imgs ? imgs.split('|') : []
					return list
				}
			}
		},
		created() {
			this.getList()
		},
		methods: {
			setShowType(index) {
				this.openIndex = index
				this.showType = "card"
			},
			setOpen(index) {
				this.openIndex = this.openIndex == index ? -1 : index
			},
			handleRefresh(e) {
				this.triggered = true;
				setTimeout(() => {
					this.query.page = 1
					this.list = []
					this.getList()
				}, 500)
			},
			lower() {
				if (this.list.length < this.total) {
					this.query.page = this.query.page + 1
					this.getList()
				} else {
					this.finish = true
					this.loading = false
				}
			},
			getList() {
				this.loading = true
				this.$http.post('/data/api.auth.User/inspectionList', { //inspectionList
					device_id: this.info.id,
				}).then(res => {

					this.list = [...this.list, ...res.data.list]
					this.total = res.data.page.total || 0
					this.loading = false
					this.triggered = false;
					this.finish = true
				}).catch(() => {
					this.loading = false
				})
			},

		}
	}
</script>

<style scoped lang="scss">
	.list {
		background-color: #FFFFFF;
		color: #333333;
		padding: 0 20rpx;
		font-size: 28rpx;
		height: 88rpx;

		&.head {
			color: #ADADAD;
			border-top: 1px solid #E9E9E9;
			border-bottom: 1px solid #E9E9E9;
			height: 104rpx;

		}

		.list_son {

			margin: 10rpx auto;
			overflow: hidden; //超出的文本隐藏
			display: -webkit-box;
			-webkit-line-clamp: 1; // 设置从第几行后开始省略
			-webkit-box-orient: vertical;

			&:nth-child(1) {
				width: 40%;
			}

			&:nth-child(2) {
				width: 40%;
			}

			&:nth-child(3) {
				width: 20%;
			}

		}
	}

	.card {
		border-radius: 20px;
		background-color: #FFFFFF;
		margin: 20rpx;
		padding: 20rpx 30rpx;
	}

	.textarea {
		background-color: #F5F5F5;
		border-radius: 10rpx;
		padding: 20rpx;
	}

	.line {
		background-color: #E9E9E9;
		height: 1px;
		margin: 15rpx 0;
	}

	.icon {
		transition-duration: 0.3s;
		width: 40rpx;
		height: 40rpx;

		&.open {
			transform: rotate(180deg);
		}
	}

	.imgs_list {
		padding: 5px 0;
		justify-content: flex-start;

		.imgs_son {
			margin: 0 8px 0 0;
			border-radius: 4px;
			overflow: hidden;
		}
	}

	.cell {
		min-height: 60rpx;

		.label {
			color: #333333;
		}

		.value {
			flex: 1;
			justify-content: flex-start;
			color: #ADADAD;
		}
	}

	.tags {
		justify-content: flex-start;
		margin-bottom: 10rpx;

		.tags_son {
			padding: 0 20rpx;
			height: 70rpx;
			border: 1px solid #ADADAD;
			border-radius: 10rpx;
		}
	}

	.btn {
		width: 94rpx;
		height: 54rpx;
		background: rgba(79, 111, 246, 1);
		border-radius: 10rpx;
		color: #FFFFFF;

	}

	.btns {
		position: fixed;
		left: 40rpx;
		right: 40rpx;
		bottom: 50rpx;
		background: rgba(79, 111, 246, 1);
		border-radius: 10rpx;
		color: #FFFFFF;
		height: 92rpx;
	}
</style>